package com.example.dllo.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dllo.farm_doctor.R;

public class Introduction_Activity extends Activity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    //定义一个界面传呼机
    private ViewPager viewPager;

    //定义一个数组存放滑动页面集合
    private final int[] pics = {R.mipmap.splash_slide, R.mipmap.splash_slide, R.mipmap.splash_slide, R.mipmap.splash_slide};

    //定义一个图像数组
    private ImageView[] imageViews;

    private ImageView imageView;

    //目前指数
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_introduction);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明的状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }
        initView();
        initPoint();
    }

    private void initPoint() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.hy_layout);
        imageViews = new ImageView[pics.length];
        for (int i = 0; i < pics.length; i++) {
            imageViews[i] = (ImageView) linearLayout.getChildAt(i);
            imageViews[i].setEnabled(false);
            imageViews[i].setTag(i);
        }
        imageViews[currentIndex].setEnabled(true);
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new ViewPagerAdapter());
        viewPager.setOnPageChangeListener(this);
        viewPager.setCurrentItem(currentIndex);
        imageView = (ImageView) findViewById(R.id.hy_btn);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setpoints(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if (currentIndex == pics.length - 1) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    private class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return pics.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        public Object instantiateItem(ViewGroup container, int position) {
            LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            ImageView iv = new ImageView(Introduction_Activity.this);
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[position]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }
    }

    private void setpoints(int pos) {
        if (pos < 0 || pos > pics.length || currentIndex == pos) return;
        imageViews[pos].setEnabled(true);
        imageViews[currentIndex].setEnabled(false);
        currentIndex = pos;
    }

}
